package com.todo.pro.controller;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.todo.pro.entity.BaiduFaceLib;
import com.todo.pro.handle.EE;
import com.todo.pro.jwt.JwtUtil;
import com.todo.pro.res.JsonResult;
import com.todo.pro.res.model.BaiduFaceLibResult;
import com.todo.pro.service.BaiduFaceLibService;
import com.todo.pro.util.MapUtil;
import com.xiaoleilu.hutool.date.DateUtil;

/**
 * @author SiZhongxia
 * @since 2017-12-27
 */
@Controller
@RequestMapping("baiduFaceLib")
public class BaiduFaceLibController extends BaseController {

	private Logger logger = LoggerFactory.getLogger(BaiduFaceLibController.class);

	@Autowired
	private BaiduFaceLibService baiduFaceLibService;

	@RequiresAuthentication
	@RequestMapping(value = "list")
	public ModelAndView list(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("modules/baiduFaceLib/list");
		mav.addObject("csrftoken",
				JwtUtil.createToken(MapUtil.init().putVal("path", "gq-baiduFaceLib-list"), 2L * 60 * 60 * 1000));
		return mav;
	}

	@ResponseBody
	@RequiresAuthentication
	@RequestMapping(value = "data")
	public JsonResult<List<BaiduFaceLibResult>> data(HttpServletRequest request) {
		Wrapper<BaiduFaceLib> baiduFaceLibWrapper = new EntityWrapper<>();

		String keyword = request.getParameter("keyword");
		if (StringUtils.isNotBlank(keyword)) {
			baiduFaceLibWrapper.like("name", keyword);
		}

		baiduFaceLibWrapper.orderBy("create_time", false);
		List<BaiduFaceLib> baiduFaceLibs = baiduFaceLibService.selectList(baiduFaceLibWrapper);

		List<BaiduFaceLibResult> baiduFaceLibrs = new ArrayList<>();
		int count = baiduFaceLibs.size();
		if (count > 0) {
			for (BaiduFaceLib baiduFaceLib : baiduFaceLibs) {
				BaiduFaceLibResult baiduFaceLibr = new BaiduFaceLibResult();
				BeanUtils.copyProperties(baiduFaceLib, baiduFaceLibr);
				baiduFaceLibr.setId(baiduFaceLib.getId().toString());
				baiduFaceLibr.setAppId(baiduFaceLib.getAppId().toString());
				baiduFaceLibrs.add(baiduFaceLibr);
			}
		}
		return new JsonResult<List<BaiduFaceLibResult>>(count, baiduFaceLibrs);
	}

	@ResponseBody
	@RequiresAuthentication
	@RequestMapping(value = "updateField")
	public JsonResult<Object> updateField(HttpServletRequest request) {
		String csrftoken = request.getParameter("csrftoken");
		if (StringUtils.isBlank(csrftoken)) {
			return new JsonResult<Object>(false, EE.ERROR_FORM.i());
		}
		Map<String, Object> jwtData = JwtUtil.parseJWT(csrftoken);
		if (jwtData == null) {
			return new JsonResult<Object>(false, EE.ERROR_FORM_OVERDUE.i());
		}

		if (!"gq-baiduFaceLib-list".equals(jwtData.get("path"))) {
			return new JsonResult<Object>(false, EE.ERROR_FORM.i());
		}

		String id = request.getParameter("id");
		if (StringUtils.isBlank(id)) {
			return new JsonResult<Object>(false, EE.INVALID_PARAMETERS.i());
		}

		String field = request.getParameter("field");
		if (StringUtils.isBlank(field)) {
			return new JsonResult<Object>(false, EE.ERROR_FORM.i());
		}

		String value = request.getParameter("value");
		if (StringUtils.isBlank(value)) {
			value = "";
		}

		BaiduFaceLib baiduFaceLib = baiduFaceLibService.selectById(id);
		if (baiduFaceLib == null) {
			return new JsonResult<Object>(false, EE.INVALID_PARAMETERS.i());
		}
		if ("name".equals(field)) {
			baiduFaceLib.setName(value);
		} else {
			return new JsonResult<Object>(false, EE.ERROR_FORM.i());
		}
		baiduFaceLib.setUpdateTime(DateUtil.date());
		if (!baiduFaceLibService.updateById(baiduFaceLib)) {
			return new JsonResult<Object>(false, EE.ERR_FOR_WAIT.i());
		}

		return new JsonResult<Object>(true, EE.SUCCESS_OP.i());
	}

	@RequiresAuthentication
	@RequestMapping(value = "add")
	public ModelAndView add(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("modules/baiduFaceLib/add");
		mav.addObject("csrftoken",
				JwtUtil.createToken(MapUtil.init().putVal("path", "gq-baiduFaceLib-add"), 30L * 60 * 1000));
		return mav;
	}

	@ResponseBody
	@RequiresAuthentication
	@RequestMapping(value = "save")
	public JsonResult<Object> save(HttpServletRequest request) {
		String csrftoken = request.getParameter("csrftoken");
		if (StringUtils.isBlank(csrftoken)) {
			return new JsonResult<Object>(false, EE.ERROR_FORM.i());
		}
		Map<String, Object> jwtData = JwtUtil.parseJWT(csrftoken);
		if (jwtData == null) {
			return new JsonResult<Object>(false, EE.ERROR_FORM_OVERDUE.i());
		}

		if (!"gq-baiduFaceLib-add".equals(jwtData.get("path"))) {
			return new JsonResult<Object>(false, EE.ERROR_FORM.i());
		}
		String name = request.getParameter("name");
		if (StringUtils.isBlank(name)) {
			return new JsonResult<Object>(false, EE.ERROR_FORM.i());
		}
		String appId = request.getParameter("appId");
		if (StringUtils.isBlank(appId)) {
			return new JsonResult<Object>(false, EE.ERROR_FORM.i());
		}

		BaiduFaceLib baiduFaceLib = new BaiduFaceLib();
		baiduFaceLib.setName(name);
		baiduFaceLib.setAppId(new Long(appId));
		baiduFaceLib.setCreateTime(DateUtil.date());
		baiduFaceLib.setUpdateTime(DateUtil.date());
		if (!baiduFaceLibService.insert(baiduFaceLib)) {
			return new JsonResult<Object>(false, EE.ERR_FOR_WAIT.i());
		}

		return new JsonResult<Object>(true, EE.SUCCESS_OP.i());
	}

	@ResponseBody
	@RequiresAuthentication
	@RequestMapping(value = "delete")
	public JsonResult<Object> delete(HttpServletRequest request) {
		String csrftoken = request.getParameter("csrftoken");
		if (StringUtils.isBlank(csrftoken)) {
			return new JsonResult<Object>(false, EE.ERROR_FORM.i());
		}
		Map<String, Object> jwtData = JwtUtil.parseJWT(csrftoken);
		if (jwtData == null) {
			return new JsonResult<Object>(false, EE.ERROR_FORM_OVERDUE.i());
		}

		if (!"gq-baiduFaceLib-list".equals(jwtData.get("path"))) {
			return new JsonResult<Object>(false, EE.ERROR_FORM.i());
		}

		String[] ids = request.getParameterValues("ids[]");
		if (ids == null || ids.length == 0) {
			return new JsonResult<Object>(false, EE.SELECT_LEAST_ONE_RECORD.i());
		}

		Set<Long> deleteLoginPicIds = new HashSet<>();
		for (String baiduFaceLibId : ids) {
			deleteLoginPicIds.add(new Long(baiduFaceLibId));
		}

		if (!baiduFaceLibService.deleteBatchIds(deleteLoginPicIds)) {
			return new JsonResult<Object>(false, EE.ERR_FOR_WAIT.i());
		}

		return new JsonResult<Object>(true, EE.SUCCESS_OP.i());
	}

}
